// pages/address/form/form.js
//工具-->构建npm
import {
  areaList
} from '../../../miniprogram_npm/@vant/area-data/index'
import {
  postJson,
  postParam
} from '../../../utils/service'
import Notify from '../../../miniprogram_npm/@vant/weapp/notify/notify'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    popShow: false,
    areaList,
    selectedArea: null,
    address: null,
    receiverName: null,
    receiverPhone: null,
    nameErrMsg: null,
    phoneErrMsg: null,
    addressErrMsg: null,
    areaErrMsg: null,
    id: null,
    loading: false
  },
  onAreaConfrim: function (event) {
    const areas = event.detail.values
    if (areas) {
      const selectedArea = areas.map(i => i.name)
      this.setData({
        area: selectedArea.join(''),
        selectedArea,
        popShow: false
      })
    }
  },
  onAreaCancel: function () {
    this.setData({
      popShow: false
    })
  },
  togglePopShow: function () {
    const p = this.data.popShow
    this.setData({
      popShow: !p
    })
  },
  onTextChange: function (event) {
    const ds = event.target.dataset
    const name = ds.name
    this.setData({
      [name]: event.detail
    })
  },
  onSave: function () {
    const param = {
      receiverName: this.data.receiverName,
      receiverPhone: this.data.receiverPhone,
      address: this.data.address,
      area: this.data.selectedArea,
    }
    const sa = this.data.selectedArea
    if (sa) {
      if (sa.length > 0) {
        param.province = sa[0]
      }
      if (sa.length > 1) {
        param.city = sa[1]
      }
      if (sa.length > 2) {
        param.county = sa[2]
      }
    }
    const r = this.checkParam(param)
    if (!r) return
    if (!this.data.id) {
      this.onAppend(param)
    } else {
      this.onUpdate(param)
    }
  },
  onAppend: function (param) {
    console.info("================= append =================", param)
    const self = this
    this.setData({
      loading: true
    })
    postJson(`${app.globalData.host}/receiver`, param, {
      success: function (data) {
        console.info(data)
        app.globalData.recevier = data
        wx.navigateTo({
          url: '/pages/order/order',
        })
      },
      fail: function (event) {
        Notify({
          type: 'danger',
          message: '新增联系人错误,请检查填写内容'
        })
      },
      complete: function () {
        self.setData({
          loading: false
        })
      }
    })
  },
  onUpdate: function (param) {
    console.info("================= update =================")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkParam: function (param) {
    const nameErrMsg = param.receiverName ? null : "请输入联系人"
    const phoneErrMsg = param.receiverPhone ? null : "请输入联系电话"
    const addressErrMsg = param.address ? null : "请输入详细地址"
    const areaErrMsg = param.area ? null : "请输入所在区域"
    this.setData({
      nameErrMsg,
      phoneErrMsg,
      addressErrMsg,
      areaErrMsg
    })
    return !nameErrMsg && !phoneErrMsg && !addressErrMsg && !areaErrMsg
  }
})